﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

using AutoMapper;

using Happy.ExtentionMethods;
using Happy.Bootstrap;

namespace Happy.Bootstrap.AutoMapper
{
    /// <summary>
    /// 自动添加配置。
    /// </summary>
    public class AutoAddProfilePlugin : IBootstrapPlugin
    {
        /// <inheritdoc />
        public void Start(IBootstrapService bootstrapService, Assembly assembly)
        {
            bootstrapService.MustNotNull("bootstrapService");
            assembly.MustNotNull("assembly");

            foreach (var type in assembly.GetTypes())
            {
                if (type.IsAbstract || type.IsInterface)
                {
                    continue;
                }

                if (typeof(Profile).IsAssignableFrom(type))
                {
                    Mapper.AddProfile(Activator.CreateInstance(type) as Profile);
                }
            }
        }
    }
}
